package com.itany.mall.service.proxy;

import java.util.List;

import com.itany.mall.entity.ProductType;
import com.itany.mall.exception.ProductTypeNameExistsException;
import com.itany.mall.exception.ServiceException;
import com.itany.mall.factory.Autowired;
import com.itany.mall.factory.Component;
import com.itany.mall.jdbc.transaction.TransactionManager;
import com.itany.mall.service.ProductTypeService;

@Component("productTypeServiceProxy")
public class ProductTypeServiceProxy implements ProductTypeService {

	@Autowired
	private TransactionManager tm;

	@Autowired("productTypeServiceImpl")
	private ProductTypeService impl;

	@Override
	public List<ProductType> findAll() throws ServiceException {
		try {
			tm.beginTransaction();
			List<ProductType> list = impl.findAll();
			tm.commit();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			tm.rollback();
			throw new ServiceException();
		}
	}

	@Override
	public void addProductType(String name) throws ProductTypeNameExistsException, ServiceException {
		try {
			tm.beginTransaction();
			impl.addProductType(name);
			tm.commit();
		} catch (ProductTypeNameExistsException e) {
			tm.rollback();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			tm.rollback();
			throw new ServiceException();
		}
	}

}
